One-time Properties
Usage (Once(callable,name) ):
class someClass(object):
def anAttr(self, __dict__, attrName):
return self.foo * self.bar
anAttr = Once(anAttr, 'anAttr')
When anInstanceOfSomeClass.anAttr is accessed for the first time,
the anAttr function will be called, and saved in the instance
dictionary. Subsequent access to the attribute will return the
cached value. Deleting the attribute will cause it to be computed
again on the next access.
The name argument is optional. If not supplied, it will default
to the __name__ of the supplied callable. (So in the usage
example above, it could have been omitted.)
Once is a Meta.NamedDescriptor , so if you place an instance of it
in a class which supports descriptor naming (i.e., has a metaclass
derived from Meta.NamedDescriptors ), it will automatically know the
correct attribute name to use in the instance dictionary, even if it
is different than the supplied name or name of the supplied callable.
However, if you place a Once instance in a class which does not
support descriptor naming, and you did not supply a valid name,
attribute access will fail with a TypeError .
Methods
|
|
__get__
__init__
computeValue
copyWithName
|
|
__get__
|
__get__ (
self,
obj,
typ=None,
)
Compute the attribute value and cache it
Note: fails if attribute name not supplied or doesn't reference
this descriptor!
Exceptions
|
|
TypeError( "%s used in type which does not support NamedDescriptor" % self )
|
|
|
__init__
|
__init__ (
self,
func,
name=None,
)
|
|
computeValue
|
computeValue (
self,
obj,
instanceDict,
attrName,
)
|
|
copyWithName
|
copyWithName ( self, newName )
|
|